<!-- Add Modal //-->
<div id="addModal" class="reveal-modal medium" data-reveal>
	<div id="add_tholabah_modal"></div>
</div>
<!-- End Add Modal //-->

<!-- Delete Modal //-->
<div id="delModal" class="reveal-modal tiny" data-reveal>
	<p class="p-modal">
	<span id="hpid" class="hpid var-modal" style="display:none;"></span>
	<input type="hidden" id="id_a_kelas" value="<?php echo $id_a_kelas = $this->security->xss_clean($this->uri->segment(3));?>"/>
	Yakin ingin menghapus <span class="nama var-modal"></span> dari kelas <span class="2nd var-modal"></span> ?</p><br/><br/>
	<span id="loadingDel" style="display:none;">Sedang memproses..</span>
	<span id="errorDel" style="display:none;color:red;">Ma'af, gagal dihapus</span>
	<span class="pull-right">
		<button class="deleteUrl btn btn-xs tmb-utama" id="hapusBagiKelas"> Yakin</button> 
		<button class="btn tmb-kedua close-reveal-modal close-modal"> Tidak</button></span>
</div>
<!-- End Delete Modal //-->

<div class="row">
	<div class="col-lg-12">
		<div class="panel panel-default kotakwidget">
			<div class="panel-heading kepalakotakwidget">
				Kelas <?php foreach ($list_mk as $row) {$id_m_tingkat = $row->id_m_tingkat; echo $row->kelas;?> - <?php echo $row->th_ajaran; $id_m_th_ajaran = $row->id_m_th_ajaran; $id_m_j_pendidikan = $row->id_m_j_pendidikan;?>, Wali Kelas : <?php echo $row->nama_lengkap;}?>
				<span class="pull-right">
					<span id="jml"><span class="t-abu2"><i><?php foreach ($list_mk as $row) {echo $row->jml;}?> tholabah</i></span></span>
					<?php if($id_m_th_ajaran == id_ta_aktif()){?>
					<button data-reveal-id="addModal" data-reveal class="btn btn-sm tmb-tambah tmb-utama" onclick="add_tholabah('<?php echo $id_m_j_pendidikan;?>','<?php echo $id_a_kelas; ?>','<?php echo id_ta_kemarin(); ?>','<?php echo $id_m_tingkat;?>','<?php echo $id_m_th_ajaran;?>')"><i class="fa fa-plus"></i> Tambah</button>
					<?php } ?>
				</span>				
			</div>
				
				<div class="panel-body">
					<div class="row">
						<div class="col-md-12">
							<div id="contentBagiKelas">
							<table class="table table-condensed" id="th_ajaran">
							  <thead>
								<tr>
									<th width="30">#</th>
									<th width="80">NIT</th>
									<th>Nama Lengkap</th>
									<th>Jenis Kelamin</th>
									<th>Alamat</th>
								</tr>
							  </thead>
							  <tbody>
								<?php 
									if(count($list)>0){
										$i = 1; 
										foreach ($list as $row) {
								?>
								<tr class="hover">
									<td><?php echo $i++ ?></td>
									<td><?php echo $row->id_m_tholabah; ?></td> 
									<td><?php echo $row->nama_lengkap; ?></td> 
									<td><?php echo $row->jk; ?></td>
									<td>
										<?php echo $row->alamat; ?>
										<?php if($id_m_th_ajaran == id_ta_aktif()){?>
										<a class="editorLink pull-right" data-reveal-id="delModal" nama="<?php echo $row->nama_lengkap; ?>" 2nd="<?php foreach ($list_mk as $row_mk) {echo $row_mk->kelas.' - '.$row_mk->th_ajaran;}?>" hpid="<?php echo $row->id_b_kelas;?>" data-reveal><span class="glyphicon glyphicon-floppy-remove"></span></a>
										<?php } ?>
									</td>
								</tr>
								<?php
										}
									}else{
								?>
								<tr>
									<td colspan="6"><center>Belum ada pembagian kelas siswa pada kelas <?php foreach ($list_mk as $row) {echo $row->kelas.' - '.$row->th_ajaran;}?> </center></td>
								</tr>
								<?php
									}
								?>
							  </tbody>
							</table>
							</div>
						</div>
					</div>
				</div>
		</div>
	</div>	
</div>
<!-- /#page-wrapper -->
<script>
// Add Tholabah, Memunculkan modal untuk memilih tholabah
function add_tholabah(id_jp,id_a_kelas,id_m_th_ajaran,id_m_tingkat,id_m_th_ajaranSkrg){
	$.ajax({
		type: "POST",
			data: "id_jp="+id_jp+"&id_a_kelas="+id_a_kelas+"&id_m_th_ajaran="+id_m_th_ajaran+"&id_m_tingkat="+id_m_tingkat+"&id_m_th_ajaranSkrg="+id_m_th_ajaranSkrg,
			url : "<?php echo site_url(); ?>b_kelas/add_tholabah",				
			cache: false,
			beforeSend: function(){
				$('#loading_th').show();
				$('#error_th').hide();
			}, 
			success: function(response){
				$('#add_tholabah_modal').html(response);
				$('#loading_th').hide();					
			}, 
			error:function(){
				$('#error_th').show();
				$('#loading_th').hide();
			}
	});
	return false;
}
// Ajax Complete
$(document).ajaxComplete(function(){
	// Show Hide Radio Button
	// Dengan sebelumnya div#naik dikasih style display: hide;	
	$('input[type="radio"]').click(function(){		
	    if($(this).attr("value")=="baru"){
	        $("div#baru").show('slow');
	        $("div#th_lalu").hide('slow');
	        $("div#pindah").hide('slow');
	    }
	    if($(this).attr("value")=="th_lalu"){
	       $("div#th_lalu").show('slow');
	       $("div#baru").hide('slow');
	       $("div#pindah").hide('slow');
	    }
	    if($(this).attr("value")=="pindah"){
	        $("div#pindah").show('slow');
	        $("div#baru").hide('slow');
	        $("div#th_lalu").hide('slow');
	    }
	});

	// Checkbox dalam bentuk table row
    $('.diklik tr').click(function(event) {
        if (event.target.type !== 'checkbox') {
            $(':checkbox', this).trigger('click');
        }
    });

    $("input[type='checkbox']").change(function (e) {
        if ($(this).is(":checked")) {
            $(this).closest('tr').addClass("highlight_row");
        } else {
            $(this).closest('tr').removeClass("highlight_row");
        }
    });	
	// Save dari tholabah baru
	$('#SaveTholabahBaru').bind('submit', function(event) {
		var link = $(this).attr('action');
		var id_a_kelas = $("#id_a_kelas").val();
		$.ajax({
			url: link,
			type: "POST",
			data: $(this).serialize(),
			cache: false,
			beforeSend: function(){
				$('#errorAdd').hide();
				$('#loadingAdd').show();
			},          
			success: function() {
				var $container = $("#contentBagiKelas");
				$container.load("<?php echo site_url(); ?>b_kelas/refresh/"+id_a_kelas);

				var $container = $("#jml");
				$container.load("<?php echo site_url(); ?>b_kelas/refresh_jml/"+id_a_kelas);
				
				$('#loadingAdd').hide();
				$('#addModal').foundation('reveal', 'close');
			},
			error:function(){
				$('#loadingAdd').hide();
				$('#errorAdd').show();
			}
		});
		return false; // dont post it automatically
	});
	// Combo pilih nama kelas
	$("#id_m_kelas").change(function(){
		var id_a_kelas = $("#id_a_kelas").val();
		var id_m_kelas = $("#id_m_kelas").val();
		var id_m_th_ajaran = $("#id_m_th_ajaran").val();
		var id_m_th_ajaranSkrg = $("#id_m_th_ajaranSkrg").val();
		$.ajax({
			type: "POST",				
			data: "id_a_kelas="+id_a_kelas+"&id_m_kelas="+id_m_kelas+"&id_m_th_ajaran="+id_m_th_ajaran+"&id_m_th_ajaranSkrg="+id_m_th_ajaranSkrg,
			url : "<?php echo site_url(); ?>b_kelas/list_tholabah_lama",
			beforeSend: function(){
				$('#loadingThLalu').show();
				$('#errorThLalu').hide();
			},
			success: function(msg){
				$('#loadingThLalu').hide();
				$('#tholabah_lama').html(msg);
				//alert('ajax called');
			},
			error:function(){
				$('#loadingThLalu').hide();
				$('#errorThLalu').show();
			}
		});		
		stopPropagation();		
	});

	// Combo pilih kelas pada th, tingkat dan jenjang yang sama
	$("#id_m_kelasSkrg").change(function(){
		var id_m_kelasSkrg 		= $("#id_m_kelasSkrg").val();
		var id_m_tingkat   		= $("#id_m_tingkat").val();
		var id_m_th_ajaranSkrg  = $("#id_m_th_ajaranSkrg").val();
		var id_a_kelas  		= $("#id_a_kelas").val();
		$.ajax({
			type: "POST",				
			data: "id_m_kelasSkrg="+id_m_kelasSkrg+"&id_m_tingkat="+id_m_tingkat+"&id_m_th_ajaranSkrg="+id_m_th_ajaranSkrg+"&id_a_kelas="+id_a_kelas,
			url : "<?php echo site_url(); ?>b_kelas/list_tholabah_pindah",
			cache: false,
			beforeSend: function(){
				$('#loading').show();
				$('#error').hide();
			},
			success: function(msg){
				$('#loading').hide();
				$('#tholabah_pindah').html(msg);
			},
			error:function(){
				$('#loading').hide();
				$('#error').show();
			}
		});
		stopPropagation();
	});
	// Save dari tholabah kelas tahun lalu
	$('#SaveTholabahThLalu').bind('submit', function(event) {
		var link = $(this).attr('action');
		var id_a_kelas = $("#id_a_kelas").val();
		$.ajax({
			url: link,
			type: "POST",
			data: $(this).serialize(),
			cache: false,
			beforeSend: function(){
				$('#errorNaik').hide();
				$('#loadingNaik').show();
			},          
			success: function() {
				var $container = $("#contentBagiKelas");
				$container.load("<?php echo site_url(); ?>b_kelas/refresh/"+id_a_kelas);

				var $container = $("#jml");
				$container.load("<?php echo site_url(); ?>b_kelas/refresh_jml/"+id_a_kelas);
				
				$('#loadingNaik').hide();
				$('#addModal').foundation('reveal', 'close');
			},
			error:function(){
				$('#loadingNaik').hide();
				$('#errorNaik').show();
			}
		});
		return false; // dont post it automatically
	});
	// Pindah tholabah / Update kelas pada tahun, tingkat dan jenjang yang sama
	$('#UpdateTholabahPindah').bind('submit', function(event) {
		var link = $(this).attr('action');
		var id_a_kelas = $("#id_a_kelas").val();
		$.ajax({
			url: link,
			type: "POST",
			data: $(this).serialize(),
			cache: false,
			beforeSend: function(){
				$('#errorPindah').hide();
				$('#loadingPindah').show();
			},          
			success: function() {
				var $container = $("#contentBagiKelas");
				$container.load("<?php echo site_url(); ?>b_kelas/refresh/"+id_a_kelas);

				var $container = $("#jml");
				$container.load("<?php echo site_url(); ?>b_kelas/refresh_jml/"+id_a_kelas);
				
				$('#loadingPindah').hide();
				$('#addModal').foundation('reveal', 'close');
			},
			error:function(){
				$('#loadingPindah').hide();
				$('#errorPindah').show();
			}
		});
		return false; // dont post it automatically
	});
});
// Delete
$("#hapusBagiKelas").on("click", function(){
	var id_b_kelas = $("#hpid").text();
	var id_a_kelas = $("#id_a_kelas").val();
		$.ajax({
			type: "POST",
			data: "id_b_kelas="+id_b_kelas,
			url : "<?php echo site_url(); ?>b_kelas/delete",				
			cache: false,
			beforeSend: function(){
				$('#loadingDel').show();
				$('#errorDel').hide();
			}, 
			success: function(){
				var $container = $("#contentBagiKelas");
				$container.load("<?php echo site_url(); ?>b_kelas/refresh/"+id_a_kelas);

				var $container = $("#jml");
				$container.load("<?php echo site_url(); ?>b_kelas/refresh_jml/"+id_a_kelas);
				
				$('#loadingDel').hide();
				$('#delModal').foundation('reveal', 'close');					
			}, 
			error:function(){
				$('#loadingDel').hide();
				$('#errorDel').show();
			}
		});
		return false; 
	});
</script>
